﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;
using System.Data;

namespace mamnontuoitho7.backend
{
    public partial class KidHealthDetails : System.Web.UI.Page
    {
        //protected Kid _kid = new Kid();
        protected DataTable _dttKidHeal;
        protected DataRow _dtrKidHeal;
        protected General _general = new General();
        private KidHeal _kidHeal = new KidHeal();

        //protected String _kidID = "";
        protected String _kidName = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            //Check Role Admin
            if (Session["memberID"].ToString() != "" && (ConstantClass.permissionInfo).IndexOf(Session["role"].ToString() + ",") != -1)
            {
                try
                {
                        if (Request.QueryString["KidID"] != null && Request.QueryString["KidFullName"] != null && Request.QueryString["hidNamePage"] != null &&
                            Request.QueryString["hidClassID"] != null && Request.QueryString["hidClassName"] != null && Request.QueryString["hidParentID"] != null)
                        {
                            hidKidID.Value = Request.QueryString["KidID"].ToString();
                            hidKidFullName.Value = Request.QueryString["KidFullName"].ToString();
                            hidNamePage.Value = Request.QueryString["hidNamePage"].ToString();
                            hidClassID.Value = Request.QueryString["hidClassID"].ToString();
                            hidClassName.Value = Request.QueryString["hidClassName"].ToString();
                            hidParentID.Value = Request.QueryString["hidParentID"].ToString();

                            //Edit Kid Health Details
                            if (Request.Form["hidEditKidInformationID"] != null && Request.Form["hidEditKidInformationID"].ToString() != "")
                            {
                                hidKidHealthInformationID.Value = Request.Form["hidEditKidInformationID"].ToString();

                                _dtrKidHeal = _kidHeal.GetOneRecordKidHealthInformation(Request.Form["hidEditKidInformationID"].ToString());
                                txtKidWeight.Text = _dtrKidHeal["KidWeight"].ToString();
                                txtWeightNotice.Text = _dtrKidHeal["WeightNotice"].ToString();
                                txtKidHeight.Text = _dtrKidHeal["KidHeight"].ToString();
                                txtHeightNotice.Text = _dtrKidHeal["HeightNotice"].ToString();
                                txtResult.Text = _dtrKidHeal["Result"].ToString();

                            }

                            //Delete Kid Health Details
                            if (Request.Form["hidKidInformationID"] != null && Request.Form["hidKidInformationID"].ToString() != "")
                            {
                                _kidHeal.DeleteKidHealInfo(Request.Form["hidKidInformationID"].ToString());
                            }

                            //Get Heal Information of Kid
                            _dttKidHeal = _kidHeal.GetKidHealInformation(hidKidID.Value);

                        }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorAdminPage);
                }
            }
            else
            {
                Response.Redirect(ConstantClass._errorLoginPage);
            }

        }


        protected void btnCreate_Click(object sender, EventArgs e)
        {
            hidKidHealthInformationID.Value = "";
            ResetInformation();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //Insert Kid Health Information 
                if (hidKidHealthInformationID.Value == "")
                {

                    if (hidKidID.Value != "")
                    {
                        bool check = _kidHeal.KidHealCreate(Session["memberID"].ToString(), hidKidID.Value, txtKidWeight.Text.Trim(), txtWeightNotice.Text.Trim(),
                                                                txtKidHeight.Text.Trim(), txtHeightNotice.Text.Trim(), txtResult.Text.Trim());

                        if (!check)
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Có lỗi hệ thống khi lưu thông tin.\n Xin vui lòng làm lại.')", true);
                        }
                        else
                        {
                            //Get Heal Information of Kid
                            _dttKidHeal = _kidHeal.GetKidHealInformation(hidKidID.Value);
                            ResetInformation();
                        }
                    }

                }
                //Update Kid Health Information
                else
                {
                    bool check = _kidHeal.KidHealUpdate(Session["memberID"].ToString(), hidKidHealthInformationID.Value, txtKidWeight.Text.Trim(), txtWeightNotice.Text.Trim(),
                                                            txtKidHeight.Text.Trim(), txtHeightNotice.Text.Trim(), txtResult.Text.Trim());

                    if (!check)
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Có lỗi hệ thống khi lưu thông tin.\n Xin vui lòng làm lại.')", true);
                    }
                    else
                    {
                        //Get Heal Information of Kid
                        _dttKidHeal = _kidHeal.GetKidHealInformation(hidKidID.Value);
                    }
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorAdminPage);
            }

        }


        private void ResetInformation()
        {
            txtKidHeight.Text = "";
            txtKidWeight.Text = "";
            txtHeightNotice.Text = "";
            txtWeightNotice.Text = "";
            txtResult.Text = "";
        }

    }
}